#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from enum import Enum

from common.env_common import get_install_head_path
from common.logger import Logger

log = Logger().get_logger("gaussdbt_plugin.log")


class RoachConstant:
    PORT = 6812
    ROACH_BACKUP = "/GaussRoach.py -t backup --mode cluster --media-type DISK --compression-level 1"
    ROACH_SHOW = "/GaussRoach.py -t show --mode cluster --media-type DISK"
    ROACH_STOP = "/GaussRoach.py -t stop"
    ROACH_HELP = "/GaussRoach.py --help"
    ROACH_META_FILE_NAME = "backupset_disk.ini"
    ROACH_LOG_META_FILE_NAME = "backupset_arch_disk.ini"
    ROACH_DATA = "mediadata"
    ROACH_ARCH = "roach_arch"
    ROACH_RESTORE = "/GaussRoach.py -t restore --mode cluster --media-type DISK"
    ROACH_START = "/GaussRoach.py -t start --media-type DISK"
    ROACH_COPY_DELETE = "/GaussRoach.py -t delete --mode cluster --media-type DISK"
    ROACH_LOG_COPY_DELETE = "/GaussRoach.py -t delete --mode cluster --media-type DISK --arch"
    ROACH_DATA_FILE_PATH = "/mnt/databackup/GaussDBT/data"
    ROACH_META_FILE_PATH = "/mnt/databackup/GaussDBT/meta"
    ROACH_LOG_FILE_PATH = "/mnt/databackup/GaussDBT/log"

    ROACH_OLD = "/tmp/oceanprotect/GaussDBT"
    ROACH_OLD_DATA_FILE_PATH = "/tmp/oceanprotect/GaussDBT/data"
    ROACH_OLD_META_FILE_PATH = "/tmp/oceanprotect/GaussDBT/meta"
    ROACH_OLD_LOG_FILE_PATH = "/tmp/oceanprotect/GaussDBT/log"

    ROACH_DATA_DEL_FILE_PATH = "/mnt/databackup/GaussDBT/delete/data"
    ROACH_META_DEL_FILE_PATH = "/mnt/databackup/GaussDBT/delete/meta"
    ROACH_DATA_FC_FILE_PATH = "/mnt/databackup/GaussDBT/fibre/data"
    ROACH_META_FC_FILE_PATH = "/mnt/databackup/GaussDBT/fibre/meta"
    ROACH_LOG_FC_FILE_PATH = "/mnt/databackup/GaussDBT/fibre/log"
    MOUNT_BUSY_NUM = 10
    AUTO_FULL_BACKUP_FILE_NAME = "autoFullBackup"
    FIRST_TIME_FILE_NAME = "firstTime"
    MIN_PARALLEL_PROCESS = 1
    MAX_PARALLEL_PROCESS = 16


class BackupStepEnum(str, Enum):
    PRE_TASK = "BackupPrerequisite"
    PRE_TASK_PROGRESS = "BackupPrerequisiteProgress"
    PRE_BACKUP_PROGRESS = "BackupPrePareProgress"
    BACKUP = "Backup"
    BACKUP_PROGRESS = "BackupProgress"
    POST_TASK = "BackupPostJob"
    POST_TASK_PROGRESS = "BackupPostJobProgress"
    STOP_TASK = "AbortJob"
    STOP_TASK_PROGRESS = "AbortJobProgress"
    QUERY_BACKUP_COPY = "QueryBackupCopy"


RestoreMainEnum = ["RestorePrerequisite", "Restore", "RestorePost"]

RestoreProgressEnum = ["RestoreProgress", "RestorePostProgress"]

ClusterStatus = ["Normal", "Abnormal", "Unavailable", "Degraded"]

FailCases = ["unavailable", "Failed", "failed", "not match backupset path"]

AREA_PARENT_PATH = '/mnt/databackup'

IS_CLONE_FILE_SYSTEM = 'is_clone_file_system'

# Agent CFG文件路径，用于读取用户制定挂载路径
AGENT_CFG_FILE = f"{get_install_head_path()}/DataBackup/ProtectClient/ProtectClient-E/conf/agent_cfg.xml"


class Env:
    USER_NAME = "application_auth_authKey"


class GaussDBTReportDBLabel:
    # 数据保护代理主机（{0}）正在执行备份子任务，执行进度：{1}%。
    BACKUP_PROGRESS_CHECKING = "dameng_plugin_backup_subjob_running_label"


class ErrorCode(int, Enum):
    ERROR_INTERNAL_ERROR = 1593988923
    ERROR_INCREMENT_TO_FULL = 1577209901
    ERROR_DIFFERENT_VERSION = 1577209971
    ERROR_DIFFERENT_TOPO = 1577209972
    ERROR_DIFFERENT_USER = 1577209973
    ERROR_MOUNT_PATH = 1577209974
    ERR_DATABASE_STATUS = 1577210000
    ERROR_USER_NOT_EXIST_CLUSTER = 0x5E0250D7
    ERR_RESTORED = 1577210101


class NormalErr(int, Enum):
    NO_ERR = 0
    FALSE = -1
    WAITING = -2


class ProgressInfo(str, Enum):
    START = "Start to do restore"
    FAILED = "Failed to do task, unique id: TU34IHS"
    SUCCEED = "Succeed to do task, unique id: TU34IHS"


class CMDResult(str, Enum):
    SUCCESS = "0"
    FAILED = "1"


class EnumPathType(int, Enum):
    INVALID_TYPE = 0
    DIR_TYPE = 1
    FILE_TYPE = 2
    LINK_TYPE = 3


RETRY_TIMES = 3

RETRY_WAIT_SECONDS = 30

WHITE_LIST = '/mnt/databackup/GaussDBT/'

SUPPORT_VERSION = 151

SUPPORT_B_VERSION = 27

JSON_SCHEMA_PATH = "gaussdbt/jsonschema/gaussdbt_cluster_define.json"